/**
 * @fileOverview 支付平台1.0
 * 兴趣点轨迹下载业务逻辑
 * @author fengbj
 */
if (!window.mapbar) {
    window.mapbar = {};
}
if (!window.mapbar.pay) {
    window.mapbar.pay = {};
}
if (!window.mapbar.pay.logon) {
    window.mapbar.pay.logon = {};
}

(function(_this){
    $.extend(_this , {
        reged : false,
        indexLogon : function(){

            if(this.logonCheck("userName" , "pwd")){
                return true;
            }else{
                return false;
            }
        },
        logonOut : function(){
            _this.delAllCookie();
            location.href = '/index.action';
        },
        buyLogon : function(){
            
            if(this.logonCheck("buyUserName" , "buyPwd")){
                //alert("提交");
                var store = 1;
                if($("#rememberme2").attr("checked") == true){
                    store = 30;
                }
                $.ajax({
                    url:"/json/ajaxLogon.action",
                    type : "post",
                    data : {
                        "userName" : $("#buyUserName").val() ,
                        "pwd" : $("#buyPwd").val() ,
                        "store" : store
                    },
                    cache : false,
                    dataType : "json",
                    success : function(result){
                        if(typeof result != "undefined" && result != null && typeof result.result != "undefined"){
                            if("success" == result.result){
                                //alert("登录成功！");
                                location.href="/buySelectPriceProcess.action?price="+$("input[name='price'][checked]").val();
                            }else{
                                alert(result.result);
                                try{
                                   
                                    window.flag = false;
                                    
                                    setTimeout(function(){
                                        window.flag=true;
                                    } , 500);
                                }catch(e){
                                    alert(e);
                                }
                               
                            }

                        }else{
                            alert("登录失败！");
                        }

                    },
                    error : function(e){
                        alert("登录失败！");
                    }
                });
            }else{
                return false;
            }


        },
        logonCheck : function(userNameId , pwdId){

            if(typeof userNameId == "undefined" || typeof pwdId == "undefined"
                || userNameId == null || pwdId == null)
                {
                return false;
            }
               
            var userName = $.trim($("#"+userNameId).val());
            var pwd = $.trim($("#"+pwdId).val());

            if(userName == null || userName == ""){
                alert("用户名不能为空！");
                $("#"+userNameId).focus();
                return false;
            }else if(pwd == null || pwd == ""){
                alert("密码不能为空！");
                $("#" + pwdId).focus();
                return false;
            }else{
                return true;
            }
        },
        setCookie : function(name, value, option) {
            //用于存储赋值给document.cookie的cookie格式字符串
            var str = name + "=" + escape(value);
            if (option) {
                //如果设置了过期时间
                if (option.expireDays) {
                    var date = new Date();
                    var ms = option.expireDays*24*3600*1000;
                    date.setTime(date.getTime() + ms);
                    str += "; expires=" + date.toGMTString();
                };
                if (option.path) {
                    str += "; path=" + option.path;
                }
                if (option.domain) {
                    str += "; domain=" + option.domain;
                }
                if (option.secure) {
                    str += "; true";
                }
            }
            document.cookie = str;
        },
        setMCookie : function(name, value, option) {
            var domain = location.href.indexOf("mapbar.com") == -1 ? "" : "mapbar.com";
            if (!option) {
                option = {
                    expireDays : '30',
                    domain : domain,
                    path : '/'
                };
            } else {
                option.domain = domain;
            }
            _this.setCookie(name, value, option);
        },
        getCookie : function(name)//取cookies函数
        {
            var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
            if(arr != null) return unescape(arr[2]);
            return null;

        },
        delCookie : function(name)//删除cookie
        {
            var exp = new Date();
            exp.setTime(exp.getTime() - 1);
            var cval="";//_this.getCookie(name);
            if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
        },
        delAllCookie : function(){
            _this.setMCookie("userId","");
            _this.setMCookie("userIdc","");
            _this.setMCookie("userName","");
        },
        regUserNameCheck : function(){

            var userName = $("#regUserName").val();
            if(typeof userName == "undefined" || userName == null || $.trim(userName) ==　""){

                alert("用户名不能为空！");
                $("#regUserName").focus();
                return false;
            }else if($.trim(userName).length < 4 || $.trim(userName).length > 20){
                alert("用户名长度为4-20！");
                $("#regUserName").focus();
                return false;
                 
            }else{
                return true;
            }
        },
        regPwdCheck : function(){

            var pwd = $("#regPwd").val();
            if(typeof pwd == "undefined" || pwd == null || $.trim(pwd) == ""){

                alert("密码不能为空！");
                $("#regPwd").focus();
                return false;
            }else if($.trim(pwd).length < 6 || $.trim(pwd).length > 16 ){
                alert("密码长度为6-16！");
                $("#regPwd").focus();
                return false;
            }else{
                return true;
            }

        },
        regPwdTwoCheck : function(){

            var pwd = $("#regPwd").val();
            var pwdTwo = $("#regPwdTwo").val();
            if(typeof pwdTwo == "undefined" || pwdTwo == null || $.trim(pwdTwo) == ""){

                alert("请确认密码！");
                $("#regPwdTwo").focus();
                return false;
            }else if($.trim(pwdTwo) != $.trim(pwd)){
                alert("两次密码输入不一致！");
                $("#regPwdTwo").focus();
                return false;
            }else{
                return true;
            }

        },
        regEMailCheck : function(){

            var email = $("#regEMail").val();
            //alert(email + "\n" +/.+@.+/i.exec(email))
            if(typeof email == "undefined" || email == null || $.trim(email) == ""){

                alert("电子邮件不能为空！");
                $("#regEMail").focus();
                return false;
            }else if(/.+@.+/i.exec(email) != null){
                return true;
            }else{
                alert("电子邮件不正确！");
                $("#regEMail").focus();
                return false;
            }

        },
        regPhoneCheck : function(){

            var phone = $("#regPhone").val();
            
            if(typeof phone == "undefined" || phone == null || $.trim(phone) == ""){

                return true;
            }else if(/\d{11}/.exec($.trim(phone)) != null){
                return true;
            }else{
                alert("手机号码不正确！");
                $("#regPhone").focus();
                return false;
            }

        },
        regSerialCheck : function(){
            
            var s1 = $("#regSerial1").val();
            var s2 = $("#regSerial2").val();
            var s3 = $("#regSerial3").val();
            var s4 = $("#regSerial4").val();
            var s = $.trim(s1) + $.trim(s2) + $.trim(s3) +$.trim(s4);
            if(s == null || s == ""){
                return true;
            }else if(/[0-9A-Za-z]{16}/i.test(s)){
                return true;
            }else{
                return false;
            }



        },
        reg : function(){

            if(_this.reged){
                return false;
            }
            _this.reged = true;
            if(_this.regUserNameCheck() &&
                _this.regPwdCheck() &&
                _this.regPwdTwoCheck() &&
                _this.regEMailCheck() &&
                _this.regPhoneCheck() &&
                _this.regSerialCheck()){

                var userName = $.trim($("#regUserName").val());
                var pwd = $.trim($("#regPwd").val());
                var email = $.trim($("#regEMail").val());
                var phone = $.trim($("#regPhone").val());

                var s1 = $("#regSerial1").val();
                var s2 = $("#regSerial2").val();
                var s3 = $("#regSerial3").val();
                var s4 = $("#regSerial4").val();
                var s = $.trim(s1) + $.trim(s2) + $.trim(s3) +$.trim(s4);
                var type = $("radio[name='regType']checked").val();
                //alert("注册");
                $.ajax({
                    url : "/json/reg.action",
                    type : "post",
                    data : {
                        userName : userName ,
                        userPwd : pwd ,
                        userEmail : email ,
                        serial : s,
                        type : type
                    },
                    dataType : "json",
                    success : function(msg){
                        if(typeof msg == "undefined" || typeof msg.result == "undefined" || msg.result == null || $.trim(msg.result) == ""){
                            alert("注册失败！");
                        }else if("success" == msg.result){
                            location.href="/buySelectPriceProcess.action?price="+$("input[name='price'][checked]").val();
                        }else{
                            alert(msg.result);
                        }
                        _this.reged = false;
                    },
                    error : function(e){
                        _this.reged = false;
                        alert("注册失败");

                    }

                });

            }else{
                _this.reged = false;
                return null;
            }
            
        }

    });
})(mapbar.pay.logon);